package com.example.blog.controller;


import com.example.blog.entity.Category;
import com.example.blog.mapper.CategoryMapper;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ly
 * @since 2023-08-03
 */
@Controller
@RequestMapping("/blog/category")
public class CategoryController {

    @Resource
    CategoryMapper categoryMapper;

    @Resource
    RedisTemplate redisTemplate;

    @RequestMapping("queryAll")
    @ResponseBody
    List<Category> queryAll(){
        // 检查redis缓存数据
        ListOperations list = redisTemplate.opsForList();
        List<Category> cates = list.range("cates", 0, -1);
        // 如果有则获取, 并返回
        if(cates == null || cates.isEmpty()){
            // 如果没有则查询,并存入 redis
            cates = categoryMapper.selectList(null);
            cates.forEach(c->{
                list.rightPush("cates", c);
            });
        }
        return cates;
    }

    // 如何同步分类数据: 设置超时

}

